﻿@model ShippingByTotalListModel
@using Nop.Plugin.Shipping.ByTotal.Models;
@using Nop.Web.Framework;
@using Nop.Core.Infrastructure;
@{
    Layout = "";

    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
}

<div class="content-header clearfix">
    <div class="pull-right">
        <a href="@Url.Action("Methods", "Shipping", new {area = "Admin"})" class="btn bg-blue">
            <i class="fa fa-truck"></i>
            @T("Admin.Configuration.Shipping.Methods.Manage")
        </a>
        <a href="@Url.Action("Restrictions", "Shipping", new {area = "Admin"})" class="btn bg-blue">
            <i class="fa fa-globe"></i>
            @T("Admin.Configuration.Shipping.Restrictions.Manage")
        </a>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-body">
        <div id="shipping-bytotal-grid"></div>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#shipping-bytotal-grid").kendoGrid({
                    dataSource: {
                        type: "json",
                        transport: {
                            destroy: {
                                url: "@Html.Raw(Url.Action("RateDelete", "ShippingByTotal", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Shipping.ByTotal.Controllers" }, { "area", "" } }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            read: {
                                url: "@Html.Raw(Url.Action("RatesList", "ShippingByTotal", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Shipping.ByTotal.Controllers" }, { "area", "" } }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            },
                            update: {
                                url: "@Html.Raw(Url.Action("RateUpdate", "ShippingByTotal", new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Shipping.ByTotal.Controllers" }, { "area", "" } }))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id",
                                fields: {
                                    DisplayOrder: { type: "number" },
                                    UsePercentage:{ type: "boolean" },
                                    From: { type: "number" },
                                    To: { type: "number" },
                                    ShippingChargeAmount: { type: "number" },
                                    ShippingChargePercentage: { type: "number" }
                                }
                            }
                        },
                        requestEnd: function (e) {
                            if (e.type == "update") {
                                this.read();
                            }
                        },
                        error: function (e) {
                            display_kendoui_grid_error(e);
                            this.cancelChanges();
                        },
                        pageSize: @(defaultGridPageSize),
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        pageSizes: [@(gridPageSizes)]
                    },
                    editable: {
                        confirmation: false,
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [{
                        field: "StoreId",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.Store")",
                        width: 100,
                        editor: storesDropDownEditor,
                        template: "#=StoreName#"
                    }, {
                        field: "WarehouseId",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.Warehouse")",
                        width: 100,
                        editor: warehouseDropDownEditor,
                        template: "#=WarehouseName#"
                    }, {
                        field: "CountryId",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.Country")",
                        width: 100,
                        editor: countriesDropDownEditor,
                        template: "#=CountryName#"
                    }, {
                        field: "StateProvinceId",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.StateProvince")",
                        width: 100,
                        editor: statesProvincesDropDownEditor,
                        template: "#=StateProvinceName#"
                    }, {
                        field: "ZipPostalCode",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.ZipPostalCode")",
                        width: 100
                    }, {
                        field: "DisplayOrder",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.DisplayOrder")",
                        width: 100,
                        editor: function (container, options) {
                            $('<input name="' + options.field + '" />')
                                    .appendTo(container)
                                    .kendoNumericTextBox({
                                        format: "{0:n0}",
                                        decimals: 0
                                    });
                        }
                    }, {
                        field: "ShippingMethodId",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.ShippingMethod")",
                        width: 100,
                        editor: shippingMethodsDropDownEditor,
                        template: "#=ShippingMethodName#"
                    }, {
                        field: "From",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.From")",
                        width: 100
                    }, {
                        field: "To",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.To")",
                        width: 100
                    }, {
                        field: "UsePercentage",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.UsePercentage")",
                        width: 100,
                        attributes: { style: "text-align:center" },
                        template: "<input type='checkbox' disabled='disabled' name='UsePercentage' # if(UsePercentage) {# checked='checked' #}  # />"
                    }, {
                        field: "ShippingChargePercentage",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.ShippingChargePercentage")",
                        width: 100
                    }, {
                        field: "ShippingChargeAmount",
                        title: "@T("Plugins.Shipping.ByTotal.Fields.ShippingChargeAmount")",
                        width: 100
                    }, {
                        command: {name: "edit", text: "@T("Admin.Common.Edit")"},
                        title: "@T("Admin.Common.Edit")",
                        width: 100
                    }, {
                        command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                        title: "@T("Admin.Common.Delete")",
                        width: 100
                    }]
                });

                $("#@Html.FieldIdFor(model => model.AddCountryId)").change(function () {
                    var selectedItem = $(this).val();
                    var ddlStates = $("#@Html.FieldIdFor(model => model.AddStateProvinceId)")
                    $.ajax({
                        cache: false,
                        type: "GET",
                        url: "@(Url.Action("GetStatesByCountryId", "Country", new RouteValueDictionary() { { "area", "Admin" } }))",
                        data: { "countryId": selectedItem, "addAsterisk": "true" },
                        success: function (data) {
                            ddlStates.html('');
                            $.each(data, function (id, option) {
                                ddlStates.append($('<option></option>').val(option.id).html(option.name));
                            });
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('@T("Plugins.Shipping.ByTotal.ManageShippingSettings.StatesFailed").Text');
                        }
                    });
                });
            });
        </script>
    </div>
</div>

<script type="text/javascript">
    @* available shipping methods for inline editing *@
    var shippingMethods = [
     @for (int i = 0; i < Model.AvailableShippingMethods.Count; i++)
     {
        var shippingMethod = Model.AvailableShippingMethods[i];
        <text>
        {
            ShippingMethodId: @shippingMethod.Value,
            ShippingMethodName: "@(shippingMethod.Text)"
        }</text>
        if (i != Model.AvailableShippingMethods.Count - 1)
        {
            <text>,</text>
        }
     }
    ];

    @* available stores for inline editing *@
    var stores = [
     @for (int i = 0; i < Model.AvailableStores.Count; i++)
     {
        var store = Model.AvailableStores[i];
        <text>
        {
            StoreId: @store.Value,
            StoreName: "@(store.Text)"
        }</text>
        if (i != Model.AvailableStores.Count - 1)
        {
            <text>,</text>
        }
     }
    ];

    @* available warehouses for inline editing *@
    var warehouses = [
     @for (int i = 0; i < Model.AvailableWarehouses.Count; i++)
     {
         var warehouse = Model.AvailableWarehouses[i];
         <text>
        {
            WarehouseId: @warehouse.Value,
            WarehouseName: "@(warehouse.Text)"
        }</text>
        if (i != Model.AvailableWarehouses.Count - 1)
        {
            <text>,</text>
        }
     }
    ];

    @* available countries for inline editing *@
    var countries = [
     @for (int i = 0; i < Model.AvailableCountries.Count; i++)
     {
        var country = Model.AvailableCountries[i];
        <text>
        {
            CountryId: @country.Value,
            CountryName: "@(country.Text)"
        }</text>
        if (i != Model.AvailableCountries.Count - 1)
        {
            <text>,</text>
        }
     }
    ];

    function shippingMethodsDropDownEditor(container, options) {
        $('<input required data-text-field="ShippingMethodName" data-value-field="ShippingMethodId" data-bind="value:' + options.field + '" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: {
                    data: shippingMethods,
                    schema: {
                        model: {
                            fields: {
                                ShippingMethodId: { type: "string" },
                                ShippingMethodName: { type: "string" },
                            }
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    }
                },
            });
    }

    function storesDropDownEditor(container, options) {
        $('<input required data-text-field="StoreName" data-value-field="StoreId" data-bind="value:' + options.field + '" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: {
                    data: stores,
                    schema: {
                        model: {
                            fields: {
                                StoreId: { type: "string" },
                                StoreName: { type: "string" },
                            }
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    }
                },
            });
    }

    function warehouseDropDownEditor(container, options) {
        $('<input required data-text-field="WarehouseName" data-value-field="WarehouseId" data-bind="value:' + options.field + '" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: {
                    data: warehouses,
                    schema: {
                        model: {
                            fields: {
                                WarehouseId: { type: "string" },
                                WarehouseName: { type: "string" },
                            }
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    }
                },
            });
    }

    function countriesDropDownEditor(container, options) {
        $('<input required data-text-field="CountryName" data-value-field="CountryId" data-bind="value:' + options.field + '" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: {
                    data: countries,
                    schema: {
                        model: {
                            fields: {
                                CountryId: { type: "string" },
                                CountryName: { type: "string" },
                            }
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    }
                },
            }).change(function(e){
                options.model.StateProvinceId = 0;
                var statesProvincesDDL = $('[data-value-field=StateProvinceId]').data("kendoDropDownList");
                statesProvincesDDL.dataSource.transport.options.read.data = { countryId: options.model.CountryId, addAsterisk: "true" }
                statesProvincesDDL.dataSource.read();
            });
    }

    function statesProvincesDropDownEditor(container, options) {
        $('<input required data-text-field="StateProvinceName" data-value-field="StateProvinceId" data-bind="value:' + options.field + '" />')
            .appendTo(container)
            .kendoDropDownList({
                autoBind: false,
                dataSource: {
                    transport: {
                        read: {
                            url:"@(Url.Action("GetStatesByCountryId", "Country", new RouteValueDictionary() { { "area", "Admin" } }))",
                            dataType: "json",
                            data: { countryId: options.model.CountryId, addAsterisk: "true" }
                        }
                    },
                    schema: {
                        model: {
                            fields: {
                                StateProvinceId: { type: "string" },
                                StateProvinceName: { type: "string" },
                            }
                        },
                        parse: function(response) {
                            var statesProvices = [];
                            for (var i = 0; i < response.length; i++) {
                                var stateProvince = {
                                    StateProvinceId: response[i].id,
                                    StateProvinceName: response[i].name
                                };
                                statesProvices.push(stateProvince);
                            }
                            return statesProvices;
                        }
                    },
                    error: function (e) {
                        display_kendoui_grid_error(e);
                        this.cancelChanges();
                    }
                }
            });
    }
</script>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <script type="text/javascript">
        $(function () {
            $("#@Html.FieldIdFor(model => model.AddUsePercentage)").click(toggleAddUsePercentage);
            toggleAddUsePercentage();

            $('#addshippingbytotalrecord').click(function () {
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("Plugin.Shipping.ByTotal.AddShippingRate")',
                    data: $(this.form).serialize(),
                    dataType: 'json',
                    success: function (data) {
                        if (data.Result == true) {
                            var grid = $("#shipping-bytotal-grid").data("kendoGrid");
                            grid.dataSource.read();
                        }
                        else {
                            alert(data.Message);
                        }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('@T("Plugins.Shipping.ByTotal.ManageShippingSettings.AddFailed").Text');
                    }
                });
                return false;
            });

            $('#resetshippingbytotalrecord').click(function () {
                $('#@Html.FieldIdFor(model => model.AddUsePercentage)').attr('checked', false);
                toggleAddUsePercentage();
            });

            $('#savegeneralsettings').click(function () {
                var postData = $(this.form).serialize();
                addAntiForgeryToken(postData);
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("Plugin.Shipping.ByTotal.SaveGeneralSettings")',
                    data: postData,
                    dataType: 'json',
                    success: function (data) {
                        alert(data.Message);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert('@T("Plugins.Shipping.ByTotal.ManageShippingSettings.AddFailed").Text');
                    }
                });
                return false;
            });
        });
        function toggleAddUsePercentage() {
            if ($('#@Html.FieldIdFor(model => model.AddUsePercentage)').is(':checked')) {
                $('#pnlAddShippingChargePercentage').show();
                $('#pnlAddShippingChargeAmount').hide();
            }
            else {
                $('#pnlAddShippingChargePercentage').hide();
                $('#pnlAddShippingChargeAmount').show();
            }
        }
    </script>

    <div class="panel panel-default">
        <div class="panel-body">
            <p>
                <b>@T("Plugins.Shipping.ByTotal.AddNewRecordTitle")</b>
            </p>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddStoreId)
                </div>
                <div class="col-md-9">
                    @Html.DropDownListFor(model => model.AddStoreId, Model.AvailableStores, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddStoreId)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddWarehouseId)
                </div>
                <div class="col-md-9">
                    @Html.DropDownListFor(model => model.AddWarehouseId, Model.AvailableWarehouses, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddWarehouseId)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddCountryId)
                </div>
                <div class="col-md-9">
                    @Html.DropDownListFor(model => model.AddCountryId, Model.AvailableCountries, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddCountryId)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddStateProvinceId)
                </div>
                <div class="col-md-9">
                    @Html.DropDownListFor(model => model.AddStateProvinceId, Model.AvailableStates, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddStateProvinceId)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddZipPostalCode)
                </div>
                <div class="col-md-9">
                    @Html.TextBoxFor(model => model.AddZipPostalCode, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddZipPostalCode)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddDisplayOrder)
                </div>
                <div class="col-md-9">
                    @Html.EditorFor(model => model.AddDisplayOrder)
                    @Html.ValidationMessageFor(model => model.AddDisplayOrder)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddShippingMethodId)
                </div>
                <div class="col-md-9">
                    @Html.DropDownListFor(model => model.AddShippingMethodId, Model.AvailableShippingMethods, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.AddShippingMethodId)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddFrom)
                </div>
                <div class="col-md-9">
                    @Html.NopEditorFor(model => model.AddFrom, Model.PrimaryStoreCurrencyCode)
                    @Html.ValidationMessageFor(model => model.AddFrom)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddTo)
                </div>
                <div class="col-md-9">
                    @Html.NopEditorFor(model => model.AddTo, Model.PrimaryStoreCurrencyCode)
                    @Html.ValidationMessageFor(model => model.AddTo)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddUsePercentage)
                </div>
                <div class="col-md-9">
                    @Html.EditorFor(model => model.AddUsePercentage)
                    @Html.ValidationMessageFor(model => model.AddUsePercentage)
                </div>
            </div>

            <div class="form-group" id="pnlAddShippingChargePercentage">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddShippingChargePercentage)
                </div>
                <div class="col-md-9">
                    @Html.EditorFor(model => model.AddShippingChargePercentage)
                    @Html.ValidationMessageFor(model => model.AddShippingChargePercentage)
                </div>
            </div>

            <div class="form-group" id="pnlAddShippingChargeAmount">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.AddShippingChargeAmount)
                </div>
                <div class="col-md-9">
                    @Html.NopEditorFor(model => model.AddShippingChargeAmount, Model.PrimaryStoreCurrencyCode)
                    @Html.ValidationMessageFor(model => model.AddShippingChargeAmount)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    &nbsp;
                </div>
                <div class="col-md-9">
                    <button type="button" id="addshippingbytotalrecord" class="btn btn-primary">
                        @T("Admin.Common.AddNew")
                    </button>
                    <button type="reset" id="resetshippingbytotalrecord" class="btn btn-default">
                        @T("Plugins.Shipping.ByTotal.Reset")
                    </button>
                </div>
            </div>
        </div>
    </div>

    <div class="panel panel-default">
        <div class="panel-body">
            <p>
                <b>@T("Plugins.Shipping.ByTotal.SettingsTitle")</b>
            </p>

            <div class="form-group">
                <div class="col-md-3">
                    @Html.NopLabelFor(model => model.LimitMethodsToCreated)
                </div>
                <div class="col-md-9">
                    @Html.EditorFor(model => model.LimitMethodsToCreated)
                    @Html.ValidationMessageFor(model => model.LimitMethodsToCreated)
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-3">
                    &nbsp;
                </div>
                <div class="col-md-9">
                    <button type="button" id="savegeneralsettings" class="btn btn-primary">
                        @T("Admin.Common.Save")
                    </button>
                </div>
            </div>
        </div>
    </div>
}